/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'marquee',
                condition: {type_id: type_id},
                model: {
                    url: _global.url.api + 'marquee',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                        '.show-offline:click': 'sethidden',
                        '.show-online:click': 'setshow',
                        '.edit_listorder:change': 'setlistorder'
                    },
                    edit: function (e) {
                        e.preventDefault();
                        var model = this;
                        var id =this.data.id;
                        var html = template("add-tpl", this.data);
                        dialog({
                            'title': '修改跑马灯',
                            'content': html,
                            'width': 700,
                            ok: function () {
                                var show = $("input[name='show']:checked").val();
                                var name = $.trim($("#title").val());
                                if (name == '') {
                                    Alert({msg: '请输入标题', type: 'error'});
                                    return false;
                                }
                                var url = $.trim($("input[name='url']").val());
                                if (url == '') {
                                    Alert({msg: '请输入url', type: 'error'});
                                    return false;
                                }
                                var listorder = $('#listorder').val();
                                if (new Model({
                                        url: _global.url.api + 'marquee',
                                        data: {show: show, listorder: listorder, name: name, url: url, id:id }
                                    }).put()) {
                                    _this.collection.reload();
                                }
                            }, cancel: function () {
                            }
                        }).show();


                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    setshow: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'marquee_set',
                                        data: {id: id, is_visible: 1, act: 'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    sethidden: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        dialog({
                            'content': '确定要继续吗?',
                            ok: function () {
                                if (new Model({
                                        url: _global.url.api + 'marquee_set',
                                        data: {id: id, is_visible: 0, act: 'show'}
                                    }).put()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    setlistorder: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if (new Model({
                                url: _global.url.api + 'marquee_set',
                                data: {id: id, listorder: listorder, act: 'order'}
                            }).put()) {
                            _this.collection.fetch();
                        }
                        ;
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $("#btn-add").on("click", function (e) {
                e.preventDefault();
                var html = template("add-tpl");
                dialog({
                    'title': '添加跑马灯',
                    'content': html,
                    'width': 700,
                    ok: function () {
                        var show = $("input[name='show']:checked").val();
                        var name = $.trim($("#title").val());
                        if (name == '') {
                            Alert({msg: '请输入标题', type: 'error'});
                            return false;
                        }
                        var url = $.trim($("input[name='url']").val());
                        if (url == '') {
                            Alert({msg: '请输入url', type: 'error'});
                            return false;
                        }
                        var listorder = $('#listorder').val();
                        if (new Model({
                                url: _global.url.api + 'marquee',
                                data: {type_id: type_id, show: show, listorder: listorder, name: name, url: url}
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                }).show();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});